/*
 * author: YuYongDe  2022/5/26
 */

package data

import (
	model "fiber-layout/databases/models"
	"gorm.io/gorm"
)

//迁移结构
type modelAuto struct {
	Model   ModelInterface
	Comment string
}

// AutoMigrate 自动迁移
func AutoMigrate(db *gorm.DB) {
	//要迁移的表与注释
	models := []modelAuto{
		{&model.User{}, "用户表"},
	}
	
	//循环迁移
	for _, val := range models {
		err := db.AutoMigrate(val.Model)
		if err != nil {
			panic(err)
		}
		//执行表备注
		db.Exec("comment on table " + val.Model.TableName() + " is '" + val.Comment + "'")
	}
}

// ModelInterface  获取表名
type ModelInterface interface {
	TableName() string
}
